package com.wyzz.hr.action;

import java.util.*;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Empaddconf;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Empquit;
import com.wyzz.hr.pojo.Emptype;
import com.wyzz.hr.pojo.Location;
import com.wyzz.hr.pojo.PositionBase;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DatabaseSysConfigManager;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.Pager;
import com.wyzz.hr.util.StringUtil;
import com.wyzz.hr.util.SysConfigManager;

public class SearchEmp extends ProfileAction {

	public SearchEmp() {
		dbConfigManager = DatabaseSysConfigManager.getInstance();
		batchemp = new Employee();
		page = new Pager();
		emp = new Employee();
		emp.setEmpShiftType(null);
	}

	public String execute() throws Exception {
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		try {
			if ("update".equalsIgnoreCase(page.getOperate())
					|| "delete".equalsIgnoreCase(page.getOperate())) {
				batchemp.setIds(emp.getIds().trim());
				String oper = "delete".equalsIgnoreCase(page.getOperate()) ? "\u5220\u9664"
						: "\u4FEE\u6539";
				List errors = empBo.batchUpdateOrDel(batchemp, page
						.getOperate(), getCurrentEmpNo());
				page.setOperate(null);
				if (errors != null && errors.size() > 0)
					addErrorInfo(errors);
				else if (batchemp.getIds() != null
						&& batchemp.getIds().length() > 0)
					addSuccessInfo(StringUtil.message(msgSuccOperate,
							new Object[] { oper }));
				batchemp = new Employee();
			} else {
				emp.setIds(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(msgErrorOperate);
		}
		getDrillDownList();
		empList = searchEmployee();
		Employee employee;
		for (Iterator i = empList.iterator(); i.hasNext(); employee
				.setJoinYear(MyTools.getWorkYearProfile(employee
						.getEmpJoinDate(), new Date())))
			employee = (Employee) i.next();

		if ("export".equals(searchOrExport)) {
			getClass();
			return downloadToExcel(empList, "OEmployeeBasic", outmatchModelId,
					"");
		} else {
			return "success";
		}
	}

	private List searchEmployee() {
		DetachedCriteria dcEmp = searchEmp_DC();
		addCriteria(dcEmp);
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		return empBo.searchAndExportEmp(dcEmp, page, searchOrExport);
	}

	public String getYearOrMonth(Float joinYear) {
		int value = 0;
		String result = "";
		if (joinYear.floatValue() >= 1.0F) {
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u5E74").toString();
		} else {
			joinYear = Float.valueOf(joinYear.floatValue() * 100F);
			value = Integer.parseInt((new StringBuilder()).append(
					joinYear.intValue()).append("").toString());
			result = (new StringBuilder()).append(result).append(value).append(
					"\u4E2A\u6708").toString();
		}
		return result;
	}

	public List searchNoOrName(String searchKey) {
		IEmployeeBo empBo = (IEmployeeBo) getBean("empBo");
		List result = new ArrayList();
		if (searchKey == null || searchKey.trim().length() == 0)
			return result;
		searchKey = searchKey.trim();
		String searchProperty = Employee.PROP_EMP_DISTINCT_NO;
		int i = 0;
		do {
			if (i >= searchKey.length())
				break;
			int ch = searchKey.charAt(i);
			if (ch > 255) {
				searchProperty = Employee.PROP_EMP_NAME;
				break;
			}
			i++;
		} while (true);
		String hql = (new StringBuilder()).append("select ").append(
				searchProperty).append(" from Employee where ").append(
				searchProperty).append(" like '%").append(searchKey).append(
				"%' order by ").append(searchProperty).toString();
		result = empBo.exeHqlList(hql.toString(), new int[] { 10 });
		return result;
	}

	private DetachedCriteria searchEmp_DC() {
		DetachedCriteria dc = DetachedCriteria.forClass(
				com.wyzz.hr.pojo.Employee.class, "emp");
		dc.createAlias(Employee.PROP_EMP_DEPT_NO, "empOrgDept", 1);
		dc.createAlias(Employee.PROP_EMP_PB_NO, "empPbNo", 1);
		dc.createAlias(Employee.PROP_EMP_LOCATION_NO, "empLocationNo", 1);
		dc.createAlias(Employee.PROP_EMP_TYPE, "empType", 1);
		if ("export".equals(searchOrExport)) {
			dc.createAlias(Employee.PROP_EMP_BENEFIT_TYPE, "empBenefitType", 1);
			dc.createAlias(Employee.PROP_EMP_CONTRACT, "contract", 1);
		}
		return dc;
	}

	private void addCriteria(DetachedCriteria dc) {
		if ("SUB".equals(super.authorityCondition))
			addSubDC(dc, "", 1);
		addOrders(dc, page, new String[] {
				(new StringBuilder()).append("empOrgDept.").append(
						Department.PROP_DEPARTMENT_SORT_ID).append("-down")
						.toString(),
				(new StringBuilder()).append(Employee.PROP_EMP_DISTINCT_NO)
						.append("-up").toString() });
		BaseCrit.addEmpDC(dc, "", emp.getEmpNoName());
		BaseCrit
				.addDeptDC(dc, Employee.PROP_EMP_DEPT_NO,
						Employee.PROP_EMP_PB_NO, Integer.valueOf(1), emp
								.getEmpDeptNo());
		BaseCrit.addDC(dc, Employee.PROP_EMP_STATUS, "eq",
				new Integer[] { Integer.valueOf(1) });
		BaseCrit.addDC(dc, Employee.PROP_EMP_TYPE, Emptype.PROP_ID,
				new Object[] { emp.getEmpType() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_LOCATION_NO, Location.PROP_ID,
				new Object[] { emp.getEmpLocationNo() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_PB_NO, PositionBase.PROP_ID,
				new Object[] { emp.getEmpPbNo() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_BENEFIT_TYPE, "id",
				new Object[] { emp.getEmpBenefitType() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_NAME, "like", new String[] { emp
				.getEmpName() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_GENDER, "eq", new Integer[] { emp
				.getEmpGender() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_DISTINCT_NO, "like",
				new String[] { emp.getId() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_MARITAL, "eq", new Integer[] { emp
				.getEmpMarital() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_WORK_PHONE, "like",
				new String[] { emp.getEmpWorkPhone() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_BLOOD, "eq", new String[] { emp
				.getEmpBlood() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_RESIDENCE_LOC, "like",
				new String[] { emp.getEmpResidenceLoc() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_CITY_NO, "like",
				new String[] { emp.getEmpCityNo() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_PROFILE_LOC, "like",
				new String[] { emp.getEmpProfileLoc() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_NATION, "like", new String[] { emp
				.getEmpNation() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_MOBILE, "like", new String[] { emp
				.getEmpMobile() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_POLITICS, "like",
				new String[] { emp.getEmpPolitics() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_HOME_PHONE, "like",
				new String[] { emp.getEmpHomePhone() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_SCHOOL, "like", new String[] { emp
				.getEmpSchool() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_EMAIL, "like", new String[] { emp
				.getEmpEmail() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_SPECIALITY, "like",
				new String[] { emp.getEmpSpeciality() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_IDENTIFICATION_NO, "like",
				new String[] { emp.getEmpIdentificationNo() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_DIPLOMA, "like",
				new String[] { emp.getEmpDiploma() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_CURR_ADDR, "like",
				new String[] { emp.getEmpCurrAddr() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_MSN, "like", new String[] { emp
				.getEmpMsn() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_HOME_ADDR, "like",
				new String[] { emp.getEmpHomeAddr() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_URGENT_CONTACT, "like",
				new String[] { emp.getEmpUrgentContact() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_COMMENTS, "like",
				new String[] { emp.getEmpComments() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_URGENT_CON_METHOD, "like",
				new String[] { emp.getEmpUrgentConMethod() });
		BaseCrit.addDCDateRange(dc, Employee.PROP_EMP_BIRTH_DATE, emp
				.getEmpBirthDate(), emp.getBirthDate());
		BaseCrit.addDCDateRange(dc, Employee.PROP_EMP_WORK_DATE, emp
				.getEmpWorkDate(), emp.getWorkDate());
		BaseCrit.addDCDateRange(dc, Employee.PROP_EMP_JOIN_DATE, emp
				.getEmpJoinDate(), emp.getJoinDate());
		BaseCrit.addDCDateRange(dc, Employee.PROP_EMP_CONFIRM_DATE, emp
				.getEmpConfirmDate(), emp.getConfirmDate());
		BaseCrit.addDC(dc, Employee.PROP_EMP_SHIFT_TYPE, "eq",
				new Integer[] { emp.getEmpShiftType() });
		BaseCrit.addDC(dc, Employee.PROP_EMP_SHIFT_NO, "like",
				new String[] { emp.getEmpShiftNo() });
		if (additional != null
				&& (!StringUtils.isEmpty(additional.getValue()) || !StringUtils
						.isEmpty(additional.getValue2()))) {
			Integer seq = additional.getEadcSeq();
			String propertyName = (new StringBuilder()).append("empAdditional")
					.append(seq).toString();
			if (seq != null && seq.intValue() != 0) {
				String type = additional.getEadcFieldType();
				if ("date".equals(type) || "number".equals(type)) {
					if (StringUtils.isNotEmpty(additional.getValue())
							&& StringUtils.isNotEmpty(additional.getValue2()))
						dc.add(Restrictions.between(propertyName, additional
								.getValue(), additional.getValue2()));
					else if (StringUtils.isNotEmpty(additional.getValue()))
						dc.add(Restrictions.ge(propertyName, additional
								.getValue()));
					else if (StringUtils.isNotEmpty(additional.getValue2()))
						dc.add(Restrictions.le(propertyName, additional
								.getValue2()));
				} else if ("select".equals(type))
					dc
							.add(Restrictions.eq(propertyName, additional
									.getValue()));
				else
					dc.add(Restrictions.like(propertyName, additional
							.getValue(), MatchMode.ANYWHERE));
			}
		}
	}

	private void getDrillDownList() {
		departs = getDrillDown("Department", new String[0]);
		empTypes = getDrillDown("EmpType", new String[0]);
		locations = getDrillDown("Location", new String[0]);
		additionalList = getDrillDown("Empaddconf",
				new String[] { "empAdditional" });
		ebfTypeList = getDrillDown("BenefitType", new String[0]);
		eqTypeMap = ComonBeans.getValuesToMap(Empquit.PROP_EQ_TYPE,
				new boolean[] { false });
		eqTypeMap.remove("1");
		eqReasonMap = ComonBeans.getSecondLevelSelect(eqTypeMap,
				Empquit.PROP_EQ_REASON, new boolean[0]);
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}

	public List getEmpTypes() {
		return empTypes;
	}

	public void setEmpTypes(List empTypes) {
		this.empTypes = empTypes;
	}

	public List getLocations() {
		return locations;
	}

	public void setLocations(List locations) {
		this.locations = locations;
	}

	public List getDeparts() {
		return departs;
	}

	public void setDeparts(List departs) {
		this.departs = departs;
	}

	public List getManagers() {
		return managers;
	}

	public void setManagers(List managers) {
		this.managers = managers;
	}

	public List getEmpList() {
		return empList;
	}

	public void setEmpList(List empList) {
		this.empList = empList;
	}

	public Pager getPage() {
		return page;
	}

	public void setPage(Pager page) {
		this.page = page;
	}

	public String getSearchOrExport() {
		return searchOrExport;
	}

	public void setSearchOrExport(String searchOrExport) {
		this.searchOrExport = searchOrExport;
	}

	public Employee getBatchemp() {
		return batchemp;
	}

	public void setBatchemp(Employee batchemp) {
		this.batchemp = batchemp;
	}

	public List getEbfTypeList() {
		return ebfTypeList;
	}

	public void setEbfTypeList(List ebfTypeList) {
		this.ebfTypeList = ebfTypeList;
	}

	public List getAdditionalList() {
		return additionalList;
	}

	public void setAdditionalList(List additionalList) {
		this.additionalList = additionalList;
	}

	public Empaddconf getAdditional() {
		return additional;
	}

	public void setAdditional(Empaddconf additional) {
		this.additional = additional;
	}

	public String getOutmatchModelId() {
		return outmatchModelId;
	}

	public void setOutmatchModelId(String outmatchModelId) {
		this.outmatchModelId = outmatchModelId;
	}

	public Map getEqTypeMap() {
		return eqTypeMap;
	}

	public void setEqTypeMap(Map eqTypeMap) {
		this.eqTypeMap = eqTypeMap;
	}

	public Map getEqReasonMap() {
		return eqReasonMap;
	}

	public void setEqReasonMap(Map eqReasonMap) {
		this.eqReasonMap = eqReasonMap;
	}

	private static final long serialVersionUID = 0xc53a77b0ae03eebfL;
	SysConfigManager dbConfigManager;
	private Employee emp;
	private Employee batchemp;
	private String searchOrExport;
	private Pager page;
	private List managers;
	private Map eqTypeMap;
	private Map eqReasonMap;
	private List departs;
	private List locations;
	private List ebfTypeList;
	private List empTypes;
	private List empList;
	private List additionalList;
	private Empaddconf additional;
	private String outmatchModelId;
	public final String outputIoName = "OEmployeeBasic";
	public final String inputIoName = "IEmployeeBasic";
}
